home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 23 / AACD 23.iso / AACD / Online / opennap / filter.c < prev    next >
C/C++ Source or Header  |  2001-06-08  |  4KB  |  154 lines

  1. /* Copyright (C) 2000-1 drscholl@users.sourceforge.net
  2.    This is free software distributed under the terms of the
  3.    GNU Public License.  See the file COPYING for details.
  4.  
  5.    $Id: filter.c,v 1.11 2001/02/23 19:23:08 drscholl Exp $ */
  6.  
  7. /* simple filtering mechanism to weed out entries which have too many
  8.  * matches.  this used to be hardcoded, but various servers will need
  9.  * to tailor this to suit their own needs.  see sample.filter for an
  10.  * example list of commonly occuring words
  11.  */
  12.  
  13. #include <stdlib.h>
  14. #include <stdio.h>
  15. #include <limits.h>
  16. #include <errno.h>
  17. #include <string.h>
  18. #include <errno.h>
  19. #include <ctype.h>
  20. #include "opennap.h"
  21. #include "debug.h"
  22.  
  23. #ifndef ROUTING_ONLY
  24. #include "regex.h"
  25.  
  26. HASH   *Filter = 0;
  27. static LIST *Block = 0;
  28.  
  29. static void
  30. load_filter_internal (HASH * h, const char *file)
  31. {
  32.     char    path[_POSIX_PATH_MAX];
  33.     char    buf[128], *token;
  34.     int     len;
  35.     FILE   *fp;
  36.  
  37.     snprintf (path, sizeof (path), "%s/%s", Config_Dir, file);
  38.     fp = fopen (path, "r");
  39.     if (!fp)
  40.     {
  41.         if (errno != ENOENT)
  42.             log ("load_filter_internal: fopen: %s: %s (errno %d)",
  43.                  path, strerror (errno), errno);
  44.         return;
  45.     }
  46.     while (fgets (buf, sizeof (buf) - 1, fp))
  47.     {
  48.         len = strlen (buf);
  49.         while (len > 0 && isspace (buf[len - 1]))
  50.             len--;
  51.         buf[len] = 0;
  52.         /* no need to convert to lowercase since the hash table is
  53.          * case-insensitive
  54.          */
  55.         token = STRDUP (buf);
  56.         hash_add (h, token, token);
  57.     }
  58.     fclose (fp);
  59. }
  60.  
  61. void
  62. load_filter (void)
  63. {
  64.     if (Filter)
  65.         free_hash (Filter);
  66.     Filter = hash_init (257, free_pointer);
  67.     load_filter_internal (Filter, "filter");
  68. }
  69.  
  70. void
  71. load_block (void)
  72. {
  73.     char    path[_POSIX_PATH_MAX];
  74.     char    buf[256];
  75.     char    err[256];
  76.     char    exp[256];
  77.     int     len;
  78.     FILE   *fp;
  79.     regex_t *rx;
  80.     int     line = 0;
  81.     LIST  **head = &Block;
  82.     int     n;
  83.  
  84.     log ("load_block: free'g old list");
  85.  
  86.     while (*head)
  87.     {
  88.         LIST   *ptr = *head;
  89.  
  90.         *head = (*head)->next;
  91.         regfree (ptr->data);
  92.         FREE (ptr);
  93.     }
  94.  
  95.     snprintf (path, sizeof (path), "%s/block", Config_Dir);
  96.     fp = fopen (path, "r");
  97.     if (!fp)
  98.     {
  99.         if (errno != ENOENT)
  100.             log ("load_block: fopen: %s: %s (errno %d)",
  101.                  path, strerror (errno), errno);
  102.         return;
  103.     }
  104.     log ("load_block: reading %s", path);
  105.     while (fgets (buf, sizeof (buf) - 1, fp))
  106.     {
  107.         line++;
  108.         if (buf[0] == '#')
  109.             continue;
  110.         len = strlen (buf);
  111.         while (len > 0 && isspace (buf[len - 1]))
  112.             len--;
  113.         buf[len] = 0;
  114.         snprintf (exp, sizeof (exp), "(^|[^[:alpha:]])%s($|[^[:alpha:]])",
  115.                   buf);
  116.         rx = CALLOC (1, sizeof (regex_t));
  117.         n = regcomp (rx, exp, REG_EXTENDED | REG_ICASE | REG_NOSUB);
  118.         if (n)
  119.         {
  120.             regerror (n, rx, err, sizeof (err));
  121.             log ("load_block:%s:%d:%s", path, line, err);
  122.             continue;
  123.         }
  124.         *head = CALLOC (1, sizeof (LIST));
  125.         (*head)->data = rx;
  126.         head = &(*head)->next;
  127.     }
  128.     fclose (fp);
  129.     log ("load_block: done");
  130. }
  131.  
  132. int
  133. is_filtered (char *s)
  134. {
  135.     return (hash_lookup (Filter, s) != 0);
  136. }
  137.  
  138. int
  139. is_blocked (char *s)
  140. {
  141.     LIST   *ptr = Block;
  142.  
  143.     for (; ptr; ptr = ptr->next)
  144.         if (regexec (ptr->data, s, 0, NULL, 0) == 0)
  145.         {
  146. #if 0
  147.             printf ("is_blocked: match: %s\n", s);
  148. #endif
  149.             return 1;
  150.         }
  151.     return 0;
  152. }
  153. #endif /* ! ROUTING_ONLY */
  154.